package com.msmall.community.bbs.preparer;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.Constants;
import com.msmall.community.bbs.model.Board;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.common.CascadeCollection;
import com.msmall.community.common.model.Commend;

/**
 * 论坛首页，每一个厅层版区的内容
 */
public class IndexEachBoardPreparer implements ViewPreparer{
	
	private static final Log log = LogFactory.getLog(IndexEachBoardPreparer.class);
	
	public void execute(TilesRequestContext tilesContext, AttributeContext context) {
		try{
			HttpServletRequest request = (HttpServletRequest) tilesContext.getRequest();
			Long boardid = (Long) context.getAttribute("boardid").getValue();

			CascadeCollection<Board> cs = (CascadeCollection<Board>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_BOARD_CASCADECOLLECTION);
			request.setAttribute("indexEachBoard", cs.getAllDatas().get(boardid).getCurrent());//当前的版区
			
			Map<Long, List<Commend>> image = (Map<Long, List<Commend>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_IMAGE_LIST);
			request.setAttribute("indexEachBoardImage", image.get(boardid));//左边图片，从后台推荐获得
			
			Map<Long, List<Topic>> imagebelow = (Map<Long, List<Topic>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_IMAGE_BELOW_LIST);
			request.setAttribute("indexEachBoardImageBelow", imagebelow.get(boardid));//左边图片下列表，从后台推荐获得
			
			Map<Long, List<Topic>> topnews = (Map<Long, List<Topic>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_TOPNEWS_LIST);
			request.setAttribute("indexEachBoardTopNews", topnews.get(boardid));//从后台推荐获得

			Map<Long, List<Topic>> topnewsbelow = (Map<Long, List<Topic>>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_COMMEND_INDEX_EACH_BOARD_TOPNEWS_BELOW_LIST);
			request.setAttribute("indexEachBoardTopNewsBelow", topnewsbelow.get(boardid));//从后台推荐获得
		}catch(Exception e){
			log.debug(e);
		}
	}
}
